<?php
	include('connect.php');
	$touser=$_POST['touser'];//openid
	$template_id=$_POST['template_id'];//模板ID
	// $page=$_GET['page'];//跳转页面
	$thing1=$_POST['title'];//发送内容字符集
	$time2=$_POST['time'];//发送内容字符集
	$thing3=$_POST['place'];//发送内容字符集
	$thing4=$_POST['tips'];//发送内容字符集
	$rand = rand(654, 98998989899)+rand(1,600);
	$datas = array('touser' => $touser, //发给谁
		'template_id' => $template_id,
		'page' => 'pages/map/map?id='.$touser.'&rand='.$rand,
		'miniprogram_state' => 'formal',//跳转小程序类型：developer为开发版；trial为体验版；formal为正式版；默认为正式版
		'data' => array(
			'thing1' => array(
				'value' => $thing1,
			),
			'time2' => array(
				'value' => $time2,
			),
			'thing4' => array(
				'value' => $thing3,
			),
			'thing3' => array(
				'value' => $thing4,
			),
			// 这里如果日期时间格式不对就报错，比如：{"errcode":47003,"errmsg":"argument invalid! hint: [I1D5la06238630] data.time3.value invalid"}
		)
	);
	
	$datas = json_encode($datas);
	$sql="select access_token from token";
	$notice =$db->query($sql);
	if ($notice->num_rows > 0) {
		 header("content-type:application/json;charset=utf-8");
		    $row = $notice->fetch_assoc();
			$token=$row["access_token"];
			$urls="https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={$token}";
			$rets = posturl($urls,$datas);
			$rets =json_decode($rets);
			$arr = get_object_vars($rets);
			$errcode = $arr['errcode'];
			if($errcode==0){
				$sql="update list set msgpowernum=msgpowernum-1 where useropenid='".$touser."'";//发送成功，更新次数
				$notice =$db->query($sql);
			}
			$errmsg = $arr['errmsg'];
			$json['data']=$rets;
			$json['code']=1;
			$sql="call send_log('{$touser}', '{$template_id}','{$thing1}','{$time2}','{$thing3}','{$thing4}','{$errcode}','{$errmsg}','{$rand}')";
			$notice =$db->query($sql);
			$json['resql']=$notice;
			
			echo (json_encode($json));
	}else{
		 header("content-type:application/json;charset=utf-8");
		$url_gettoken='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx075c9c546d022107&secret=2a488834a0b1b3b4450568aec536e85f';
		$token=file_get_contents($url_gettoken);//发送HTTPs请求并获取返回的数据，推荐使用curl
		$json = json_decode($token);//对json数据解码
		$arr = get_object_vars($json);
		$access_token=$arr['access_token'];//token
		$expires_in=$arr['expires_in'];//过期时间
		$time=time();
		$jsons['code']=0;
		$sql="INSERT INTO access_token (access_token,expires_in,in_time) VALUES ('{$access_token}', '{$expires_in}','{$time}' )";
		$addtoken =$db->query($sql);
		echo (json_encode($jsons));
	}

	function posturl($url,$data){
	        // $data  = json_encode($data);    
	        $headerArray =array("Content-type:application/json;charset='utf-8'","Accept:application/json");
	        $curl = curl_init();
	        curl_setopt($curl, CURLOPT_URL, $url);
	        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
	        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
	        curl_setopt($curl, CURLOPT_POST, 1);
	        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
	        curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
	        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	        $output = curl_exec($curl);
	        curl_close($curl);
			return $output;
	}
?>